package com.fm.financemanage.utils;


import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MessageDigestUtil {
    public MessageDigestUtil() {
    }

    public static String encryptMD5(byte[] data) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        messageDigest.update(data);
        return BytesToHex.fromBytesToHex(messageDigest.digest());
    }

    public static String getFileMD5(String path) throws Exception {
        FileInputStream fis = new FileInputStream(new File(path));
        DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5"));

        try {
            byte[] buffer = new byte[1024];

            for (int read = dis.read(buffer, 0, 1024); read != -1; read = dis.read(buffer, 0, 1024)) {
            }

            MessageDigest md = dis.getMessageDigest();
            String var6 = BytesToHex.fromBytesToHex(md.digest());
            return var6;
        } finally {
            dis.close();
            fis.close();
        }
    }

    public static String encryptSHA(byte[] data) throws Exception {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA");
        messageDigest.update(data);
        return BytesToHex.fromBytesToHex(messageDigest.digest());
    }

    public static String encryptSHA1(byte[] data) throws Exception {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        messageDigest.update(data);
        return BytesToHex.fromBytesToHex(messageDigest.digest());
    }

    public static byte[] initHmacKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");
        return keyGenerator.generateKey().getEncoded();
    }

    public static String encryptHmac(byte[] data, byte[] key) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
        Mac mac = Mac.getInstance("HmacSHA512");
        mac.init(secretKey);
        return BytesToHex.fromBytesToHex(mac.doFinal(data));
    }

    public static void main(String[] args) throws Exception {
        String data = "123456";
        String path = "/Users/admin/Downloads/httpd-2.4.20.tar.gz";
        System.out.println(data + " 的MD5值： " + encryptMD5(data.getBytes()));
        System.out.println(path + " 的文件MD5签名： " + getFileMD5(path));
        System.out.println(data + " 的SHA值： " + encryptSHA(data.getBytes()));
        byte[] hmacKey = initHmacKey();
        System.out.println("初始化Hmac密钥：" + BytesToHex.fromBytesToHex(hmacKey));
        System.out.println(data + " 是的Hmac加密值为： " + encryptHmac(data.getBytes(), hmacKey));
    }
}
